#include <DAVE.h>               //Declarations from DAVE Code Generation (includes SFR declaration)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "UserApp/esc_uart.h"
#include "SSC/Src/XMC_ESCObjects.h"
#include"UserApp/IO_Process.h"
#include "UserApp/user_app.h"
#include "UserApp/esc_uart.h"

#define CYCLE_TIME_uS 30000U //20ms执行一次查询反馈命令
#define TIME_WINDOW_Lo 20000U
#define TIME_WINDOW_Hi 28000U

extern uint32_t time_feedback;

int main(void)
{
	  DAVE_STATUS_t init_status;
	  SYSTIMER_STATUS_t time_status;
	  uint32_t TimerId;
	  uint8_t used_port_number=0;
	  uint32_t time_measure=0, time_delta;

	  delay_ms(1000);
	  init_status = DAVE_Init();//Initialization of DAVE APPs
	  TimerId = (uint32_t)SYSTIMER_CreateTimer(CYCLE_TIME_uS, SYSTIMER_MODE_PERIODIC, (void*)Check_Gripper_Status, NULL);
	  used_port_number=Scan_Connected_Gripper();
	  Reset_Update_Member();
	  time_status=SYSTIMER_StartTimer(TimerId);
	  if((init_status == DAVE_STATUS_FAILURE)||(TimerId==0U)||(time_status==SYSTIMER_STATUS_FAILURE))
	  {
			/* Placeholder for error handler code. The while loop below can be replaced with an user error handler. */
			XMC_DEBUG("DAVE APPs initialization failed\n");
			while(1U)
			{}
	  }

	  while(1U)
	  {
//		  HW_ClearTimer();//测量运行时间，正式版需要去掉

		  time_measure = SYSTIMER_GetTime();
		  time_delta = time_measure - time_feedback;
		  if((Check_Free_Port_Number() == used_port_number)&&(TIME_WINDOW_Lo <time_delta)&&(time_delta< TIME_WINDOW_Hi))
		  {
			  //等待到达时间窗口，且端口都空闲
			  if(!Gripper_mode_Config(TimerId))
			  {
				  //485模式
				  if(Is_Control_Update())
				  {
					  //如控制命令有更新
					  SYSTIMER_Stop();
					  ControlGripper(used_port_number);
					  SYSTIMER_Start();
//					  SYSTIMER_RestartTimer(TimerId, CYCLE_TIME_uS);
				  }
			  }else
			  {
				  //IO模式
				  Read_IO_Bus();
			  }
		  }
		  Manage_Uart_Status_LEDs();
		  MainLoop();

//		  Box_Infomation0x6100.BoxVersion = HW_GetTimer();//测量运行时间，正式版需要去掉
	  }
}
/*每次重新生成代码后需要修改的代码
if(ticks == 4294967295)
{
	ticks = 0;
}
*/


/**
 * @brief SYNC0IRQHandler() - EtherCAT Interrupt Routine for SYNC0
 *
 * <b>Details of function</b><br>
 * This routine is handling the SYNC0 Interrupts and need to call the SSC Stack
 */
void SYNC0IRQHandler (void)
{
	Sync0_Isr();
}

/**

 * @brief SYNC1IRQHandler() - EtherCAT Interrupt Routine for SYNC1
 *
 * <b>Details of function</b><br>
 * This routine is handling the SYNC1 Interrupts and need to call the SSC Stack
 */
void SYNC1IRQHandler (void)
{
	Sync1_Isr();
}

/**

 * @brief ENABLE_ESC_INT_USER() - Enabling of user specific EtherCAT Interrupt Routines
 *
 * <b>Details of function</b><br>
 * This routine is called from ECAT_APP on request of SSC stack once interrupts (sync1/sync0) need to be enabled
 */
void ENABLE_ESC_INT_USER()
{
	INTERRUPT_Enable(&INT_SYNC0);
	INTERRUPT_Enable(&INT_SYNC1);
}

/**

 * @brief DISABLE_ESC_INT_USER() - Disabling of user specific EtherCAT Interrupt Routines
 *
 * <b>Details of function</b><br>
 * This routine is called from ECAT_APP on request of SSC stack once interrupts (sync1/sync0) need to be disabled
 */
void DISABLE_ESC_INT_USER()
{
	INTERRUPT_Disable(&INT_SYNC0);
	INTERRUPT_Disable(&INT_SYNC1);
}

